package com.seari.service.Impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.seari.utils.DateUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.seari.bean.QueryProBean;
import com.seari.bean.SelectObj;
import com.seari.mapper.ProSearchMapper;
import com.seari.pojo.LagAndRiskPojo;
import com.seari.service.ProSearchService;

@Service
public class ProSearchServiceImpl implements ProSearchService {

	@Autowired
	private ProSearchMapper proSearchMapper;

	@Override
	public List<SelectObj> proSearch(@Param("proText") String proText) {
		// TODO Auto-generated method stub
		return proSearchMapper.proSearch(proText);
	}
	@Override
	public List<SelectObj> proSearchSAP(@Param("proText") String proText) {
		// TODO Auto-generated method stub
		return proSearchMapper.proSearchSAP(proText);
	}
	@Override
	public List<SelectObj> getProMajor() {
		// TODO Auto-generated method stub
		return proSearchMapper.getProMajor();
	}
	@Override
	public List<SelectObj> getProMajorSAP() {
		// TODO Auto-generated method stub
		return proSearchMapper.getProMajorSAP();
	}
	@Override
	public List<SelectObj> getProNature() {
		// TODO Auto-generated method stub
		return proSearchMapper.getProNature();
	}
	@Override
	public List<SelectObj> getProNatureSAP() {
		// TODO Auto-generated method stub
		return proSearchMapper.getProNatureSAP();
	}
	@Override
	public List<Map<String, String>> getProCount(QueryProBean queryProBean) {
		// TODO Auto-generated method stub
		List<Map<String, String>> proCounts = proSearchMapper
				.getProCount(queryProBean);
		Map<String, String> proCount = proSearchMapper.getCount(queryProBean);
		proCounts.add(proCount);
		return proCounts;
	}

	@Override
	public List<Map<String, Object>> getProCountSAP(QueryProBean queryProBean) {
		String month = queryProBean.getMonth();
		String year = queryProBean.getYear();
		String beginDate = year+month+"00";
		String endDate = DateUtil.getCurrDate("yyyyMMdd");
		queryProBean.setEndDate(endDate);
		queryProBean.setBeginDate(beginDate);
		// TODO Auto-generated method stub
		int XKCount = proSearchMapper.getXKCountSAP(queryProBean);
		int GDCounts = proSearchMapper.getGDCountSAP(queryProBean);
		List<Map<String, Object>> counts = new ArrayList<>();
		Map<String, Object> maps = new HashMap<>();
		maps.put("XKCount",XKCount);
		maps.put("GDCounts",GDCounts);
		maps.put("proCount",XKCount+GDCounts);
		counts.add(maps);
		return counts;
	}

	@Override
	public List<Map<String, String>> queryProConMoney(QueryProBean queryProBean) {
		// TODO Auto-generated method stub
		return proSearchMapper.queryProConMoney(queryProBean);
	}
	@Override
	public List<Map<String, Object>> queryProConMoneySAP(QueryProBean queryProBean) {
		String month = queryProBean.getMonth();
		String year = queryProBean.getYear();
		String beginDate = year+month+"00";
		String endDate = DateUtil.getCurrDate("yyyyMMdd");
		queryProBean.setEndDate(endDate);
		queryProBean.setBeginDate(beginDate);
		queryProBean.setYearMonth(queryProBean.getYear()+queryProBean.getMonth());
		queryProBean.setEndACDate(year+month+"31");
		Integer monthSum = proSearchMapper.queryMonthConMoneySAP(queryProBean);
		Integer yearSum = proSearchMapper.queryYearConMoneySAP(queryProBean);
		Integer monthACSum = proSearchMapper.queryMonthACConMoneySAP(queryProBean);
		Integer yearACSum = proSearchMapper.queryYearACConMoneySAP(queryProBean);

		double ratioMonth = 0;
		monthACSum = monthACSum == null?0:monthACSum;
		if(monthSum == null){
			monthSum = 0;
		}else {
			ratioMonth = Math.round( monthACSum / monthSum);
		}
		double ratioYear = 0;
		yearACSum = yearACSum == null?0:yearACSum;
		if(yearSum == null){
			yearSum = 0;
		}else {
			ratioYear = Math.round(yearACSum/yearSum);
		}
		System.err.println("ratioMonth>>>>"+ratioMonth);
		System.err.println("ratioYear>>>>"+ratioYear);

		Map<String, Object> maps = new HashMap<>();
		maps.put("costMonth",monthSum);
		maps.put("costYear",yearSum);
		maps.put("aucostMongth",monthACSum);
		maps.put("aucostYear",yearACSum);
		maps.put("ratioMonth",ratioMonth);
		maps.put("ratioYear",ratioYear);
		List<Map<String, Object>> counts = new ArrayList<>();
		counts.add(maps);
		return counts;
	}
	@Override
	public List<Map<String, String>> queryProInvestment(
			QueryProBean queryProBean) {
		// TODO Auto-generated method stub
		return proSearchMapper.queryProInvestment(queryProBean);
	}
	@Override
	public List<Map<String, Object>> queryProInvestmentSAP(
			QueryProBean queryProBean) {
		String month = queryProBean.getMonth();
		String year = queryProBean.getYear();
		String beginDate = year+month+"00";
		String endDate = DateUtil.getCurrDate("yyyyMMdd");
		queryProBean.setEndDate(endDate);
		queryProBean.setBeginDate(beginDate);
		queryProBean.setYearMonth(queryProBean.getYear()+queryProBean.getMonth());
		queryProBean.setEndACDate(year+month+"31");
		Integer monthSum = proSearchMapper.queryMonthInvestmentSAP(queryProBean);
		Integer yearSum = proSearchMapper.queryYearInvestmentSAP(queryProBean);
		Integer monthACSum = proSearchMapper.queryMonthACInvestmentSAP(queryProBean);
		Integer yearACSum = proSearchMapper.queryYearACInvestmentSAP(queryProBean);

		double ratioMonth = 0;
		monthACSum = monthACSum == null?0:monthACSum;
		if(monthSum == null){
			monthSum = 0;
		}else {
			ratioMonth = Math.round( monthACSum / monthSum);
		}
		double ratioYear = 0;
		yearACSum = yearACSum == null?0:yearACSum;
		if(yearSum == null){
			yearSum = 0;
		}else {
			ratioYear = Math.round(yearACSum/yearSum);
		}
		System.err.println("ratioMonth>>>>"+ratioMonth);
		System.err.println("ratioYear>>>>"+ratioYear);

		Map<String, Object> maps = new HashMap<>();
		maps.put("monthInvestPlan",monthSum);
		maps.put("yearInvestPlan",yearSum);
		maps.put("monthInvestAc",monthACSum);
		maps.put("yearInvestAc",yearACSum);
		maps.put("monthRatio",ratioMonth);
		maps.put("yearRatio",ratioYear);
		List<Map<String, Object>> counts = new ArrayList<>();
		counts.add(maps);
		return counts;
	}
	@Override
	public List<String> getproIdsByCompanyIdAndNatureId(Object companyId,
			Object natureId) {
		// TODO Auto-generated method stub
		return proSearchMapper.getproIdsByCompanyIdAndNatureId(companyId,
				natureId);
	}
	@Override
	public Map<String, LagAndRiskPojo> getproIdsByCompanyIdAndNatureIdSAP(QueryProBean queryProBean) {
		// TODO Auto-generated method stub
		String year = queryProBean.getYear();
		String month = queryProBean.getMonth();
		String beginDate = year+month+"00";
		String endDate = year+month+"31";
		queryProBean.setBeginDate(beginDate);
		queryProBean.setEndDate(endDate);
		List<String> NodeLists = proSearchMapper.getproIdsByCompanyIdAndNatureIdSAP(queryProBean);
		String beginFristTime = year+"0100";
		queryProBean.setBeginFristTime(beginFristTime);
		String beginFristMonth = year+"01";
		queryProBean.setBeginFristMonth(beginFristMonth);
		String endMonth = year+month;
		queryProBean.setEndMonth(endMonth);
		List<String> invList = proSearchMapper.getInvestTmpSAP(queryProBean);
		List<String> payList = proSearchMapper.getPayTmpSAP(queryProBean);

		Map<String, LagAndRiskPojo> resultMap = new HashMap<String, LagAndRiskPojo>();
		LagAndRiskPojo lagNode = new LagAndRiskPojo();
		List<String> collect = NodeLists.stream().distinct()
				.collect(Collectors.toList());
		//滞后节点
		lagNode.setCount(NodeLists.size());
		lagNode.setProids(collect);
		resultMap.put("lagNode",lagNode);

		//滞后投资
		LagAndRiskPojo lagInvert = new LagAndRiskPojo();
		lagInvert.setCount(invList.size());
		lagInvert.setProids(invList);
		resultMap.put("lagInvert",lagInvert);

		//滞后资金
		LagAndRiskPojo lagMoney = new LagAndRiskPojo();
		lagMoney.setCount(payList.size());
		lagMoney.setProids(payList);
		resultMap.put("lagMoney",lagMoney);

		//风险项目

		return resultMap;
	}
	@Override
	public List<String> getConIdsByproIds(QueryProBean queryProBean) {
		// TODO Auto-generated method stub
		return proSearchMapper.getConIdsByproIds(queryProBean);
	}

	@Override
	public List<String> getConIdsByproId(String proid) {
		// TODO Auto-generated method stub
		return proSearchMapper.getConIdsByproId(proid);
	}

	@Override
	public Map<String, LagAndRiskPojo> getProlagAnalyse(
			QueryProBean queryProBean, List<String> proIdList) {
		// 滞后项目总数ID集合
		List<String> lagProIdList = new ArrayList<String>();
		// 风险项目总数ID集合
		List<String> riskProIdList = new ArrayList<String>();

		// 滞后节点ID集合
		List<String> lagNode = new ArrayList<String>();
		// 滞后风险节点ID集合
		List<String> riskNode = new ArrayList<String>();

		// 滞后投资数ID集合
		List<String> lagInvert = new ArrayList<String>();
		// 滞后风险投资数ID集合
		List<String> riskInvert = new ArrayList<String>();

		// 滞后资金数ID集合
		List<String> lagMoney = new ArrayList<String>();
		// 滞后风险资金数ID集合
		List<String> riskMoney = new ArrayList<String>();

		String year = queryProBean.getYear();
		String month = queryProBean.getMonth();
		// 拼接日期 SQL server 日期格式化 CONVERT (datetime, '2019-06-01')
		StringBuffer sb = new StringBuffer();
		// 月份的第一天
		String date = sb.append(year).append("-").append(month).append("-01")
				.toString();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		Date thisDate = null;

		try {
			thisDate = sdf.parse(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Calendar cld = Calendar.getInstance();
		cld.setTime(thisDate);
		cld.add(Calendar.MONTH, 1);
		cld.set(Calendar.DAY_OF_MONTH, 0);
		// 获取月份的最后一天
		String lastDay = sdf.format(cld.getTime());

		String proid = null;
		Double lagNum = 0.0;
		Double riskRatio = 0.0;
		Double moneyLag = 0.0;
		Double moneyRatio = 0.0;
		int mon = Integer.parseInt(month);
		for (int i = 0; i < proIdList.size(); i++) {
			QueryProBean pb = new QueryProBean();
			proid = proIdList.get(i);
			List<String> conIdLists = proSearchMapper.getConIdsByproId(proid);
			pb.setConIdList(conIdLists);
			pb.setYear(year);
			pb.setMonth(month);
			pb.setFristDay(date);
			pb.setLastDay(lastDay);
			pb.setProId(proid);
			// 判断主路径当月节点滞后数量 result > 0 则记录此项目节点滞后
			int result = proSearchMapper.getLagNode(pb);
			if (result > 0) {
				lagProIdList.add(proid);
				riskProIdList.add(proid);
				lagNode.add(proid);
				riskNode.add(proid);
			}
			// 判断投资数滞后 及风险滞后
			Map<String, Double> lagMap = proSearchMapper.getProInvertLag(pb);
			System.out.println(lagMap);
			Double float1 = lagMap.get("lagNum");
			System.out.println(float1);
			try {
				lagNum = lagMap.get("lagNum");
				riskRatio = lagMap.get("riskRatio");
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (lagNum < 0) {
				lagInvert.add(proid);
				lagProIdList.add(proid);
			} else {

				if (mon > 0 && mon <= 3) {
					if (riskRatio < 60) {
						riskProIdList.add(proid);
						riskInvert.add(proid);
					}
				} else if (mon > 3 && mon <= 6) {
					if (riskRatio < 70) {
						riskProIdList.add(proid);
						riskInvert.add(proid);
					}
				} else if (mon > 6 && mon <= 9) {
					if (riskRatio < 80) {
						riskProIdList.add(proid);
						riskInvert.add(proid);
					}
				} else if (mon > 9) {
					if (riskRatio < 90) {
						riskProIdList.add(proid);
						riskInvert.add(proid);
					}
				}
			}

			// 判断资金数滞后 及风险滞后
			Map<String, Double> numLagMap = proSearchMapper
					.getProMoneyNumLag(pb);
			try {
				moneyLag = numLagMap.get("moneyLag");
				moneyRatio = numLagMap.get("moneyRatio");
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (moneyLag < 0) {
				lagMoney.add(proid);
				lagProIdList.add(proid);
			} else {
				if (mon > 0 && mon <= 3) {
					if (moneyRatio < 60) {
						riskMoney.add(proid);
						riskProIdList.add(proid);
					}
				} else if (mon > 3 && mon <= 6) {
					if (moneyRatio < 70) {
						riskMoney.add(proid);
						riskProIdList.add(proid);
					}
				} else if (mon > 6 && mon <= 9) {
					if (moneyRatio < 80) {
						riskMoney.add(proid);
						riskProIdList.add(proid);
					}
				} else if (mon > 9) {
					if (moneyRatio < 90) {
						riskMoney.add(proid);
						riskProIdList.add(proid);
					}
				}
			}

		}

		Map<String, LagAndRiskPojo> resultMap = new HashMap<String, LagAndRiskPojo>();
		LagAndRiskPojo lagCount = new LagAndRiskPojo();
		// 去重
		List<String> collect = lagProIdList.stream().distinct()
				.collect(Collectors.toList());
		lagCount.setCount(collect.size());
		lagCount.setProids(collect);
		resultMap.put("lagcount", lagCount);

		LagAndRiskPojo lagNodepojo = new LagAndRiskPojo();
		lagNodepojo.setCount(lagNode.size());
		lagNodepojo.setProids(lagNode);
		resultMap.put("lagNode", lagNodepojo);

		LagAndRiskPojo lagInvertPojo = new LagAndRiskPojo();
		lagInvertPojo.setCount(lagInvert.size());
		lagInvertPojo.setProids(lagInvert);
		resultMap.put("lagInvert", lagInvertPojo);

		LagAndRiskPojo lagMoneyPojo = new LagAndRiskPojo();
		lagMoneyPojo.setCount(lagMoney.size());
		lagMoneyPojo.setProids(lagMoney);
		resultMap.put("lagMoney", lagMoneyPojo);

		LagAndRiskPojo riskcountPojo = new LagAndRiskPojo();
		List<String> collect2 = riskProIdList.stream().distinct()
				.collect(Collectors.toList());
		riskcountPojo.setCount(collect2.size());
		riskcountPojo.setProids(collect2);
		resultMap.put("riskcount", riskcountPojo);

		LagAndRiskPojo riskNodePojo = new LagAndRiskPojo();
		riskNodePojo.setCount(riskNode.size());
		riskNodePojo.setProids(riskNode);
		resultMap.put("riskNode", riskNodePojo);

		LagAndRiskPojo riskInvertPojo = new LagAndRiskPojo();
		riskInvertPojo.setCount(riskInvert.size());
		riskInvertPojo.setProids(riskInvert);
		resultMap.put("riskInvert", riskInvertPojo);

		LagAndRiskPojo riskMoneyPojo = new LagAndRiskPojo();
		riskMoneyPojo.setCount(riskMoney.size());
		riskMoneyPojo.setProids(riskMoney);
		resultMap.put("riskMoney", riskMoneyPojo);

		return resultMap;
	}

	@Override
	public List<Map<String, String>> queryProName(List<String> proIdList) {
		return proSearchMapper.queryProName(proIdList);
	}

	@Override
	public List<String> queryConIdsList(QueryProBean queryProBean) {
		// TODO Auto-generated method stub
		return proSearchMapper.queryConIdsList(queryProBean);
	}

	@Override
	public List<String> queryConIdsListSAP(QueryProBean queryProBean) {
		// TODO Auto-generated method stub
		return proSearchMapper.queryConIdsListSAP(queryProBean);
	}
	@Override
	public List<String> queryProIdsListSAP(QueryProBean queryProBean) {
		// TODO Auto-generated method stub
		return proSearchMapper.queryProIdsListSAP(queryProBean);
	}
}
